﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web
{
    public partial class ProductList : System.Web.UI.Page
    {
        int cid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxProcess();

            int.TryParse(Request.QueryString["cid"], out cid);

            if (!IsPostBack)
            {
                BindData();
                GetNav(cid);
            }
        }

        protected void AjaxProcess()
        {
            if (Request.QueryString["ope"] == "addnum")
            {
                Response.Write(AddNum());
                Response.End();
            }
        }

        protected string AddNum()
        {
            int productId = 0;
            int num = 0;
            int.TryParse(Request.QueryString["productId"], out productId);
            int.TryParse(Request.QueryString["num"], out num);
            if (productId > 0)
            {
                BLL.ShoppingCar _ShoppingCarBLL = new BLL.ShoppingCar();
                BLL.Products _ProductsBLL = new BLL.Products();
                if (num != 0)
                {
                    Model.Products _ProductsModel = _ProductsBLL.GetModel(productId);
                    Model.Oitems _OitemsModel = new Model.Oitems();
                    _OitemsModel.ProductId = productId;
                    _OitemsModel.ProductNum = num;
                    _OitemsModel.ProductName = _ProductsModel.ProductName;
                    _OitemsModel.SalePrice = _ProductsModel.SalePrice;
                    _OitemsModel.RealPrice = _ProductsModel.SalePrice;
                    _OitemsModel.TotalPrice = _ProductsModel.SalePrice * num;
                    _OitemsModel.TaxFee = 0;
                    _OitemsModel.InsureFee = 0;
                    _OitemsModel.DeliveryFee = 0;
                    _OitemsModel.ProductDesc = Request.QueryString["lay"];
                    int res = _ShoppingCarBLL.UpdateShoppingCar(_OitemsModel);

                    return res.ToString();
                }
            }

            return "0";
        }

        protected void BindData()
        {
            BLL.Products proBLL = new BLL.Products();
            string strWhere = "(categoryId=" + cid + " or categoryId in(select categoryId from Categorys where parentId=" + cid + ")) and IsHid=0 and IsActive=1";
            string strOrder = "ProductId desc";
            int recordCount = proBLL.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = proBLL.GetListByPage(strWhere, strOrder, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }

        protected string nav = string.Empty;
        protected string GetNav(int id)
        {
            string curr = string.Empty;
            BLL.Categorys catBLL = new BLL.Categorys();
            Model.Categorys catModel = catBLL.GetModel(id);

            Title = catModel.CategoryName;
            MetaKeywords = catModel.CategoryName;
            MetaDescription = catModel.CategoryName;

            curr = "<span> > </span><a href=\"ProductList.aspx?cid=" + catModel.CategoryId + "\" class=\"fs-14\">" + catModel.CategoryName + " </a>";
            if (catModel.ParentId != 0)
            {
                nav = GetNav(catModel.ParentId) + curr;
            }
            else
            {
                nav = nav + curr;
            }

            return nav;
        }
    }
}